import 'package:flutter/widgets.dart';
import 'package:flutter_study/widgets/public/themes/index.dart';
import 'package:rc_widget/rc_widget.dart';

class AppPriceText extends StatelessWidget {
  const AppPriceText(
    this.price, {
    super.key,
    this.needSymbol = true,
    this.symbol = '¥',
    this.decimalDigits = 2,
    this.style,
    this.integerStyle,
    this.suffix,
    this.suffixStyle,
  });

  final String price;
  final bool needSymbol;
  final String symbol;
  final int decimalDigits;
  final TextStyle? style;
  final TextStyle? integerStyle;
  final String? suffix;
  final TextStyle? suffixStyle;

  List<String> get priceSlices {
    String fullPrice = price;
    if (!fullPrice.contains('.')) {
      fullPrice += '.${'0' * decimalDigits}';
    }
    if (fullPrice.endsWith('.')) {
      fullPrice == '0' * decimalDigits;
    }
    return fullPrice.split('.');
  }

  String get integerText => priceSlices.first;
  String get decimalText => priceSlices.last;

  @override
  Widget build(BuildContext context) {
    return Text.rich(TextSpan(
        text: needSymbol ? symbol : '',
        style: style ?? AppThemes.of().w700Text428,
        children: [
          TextSpan(
              text: integerText,
              style: integerStyle ?? TextStyle(fontSize: 32.sp)),
          TextSpan(text: '.$decimalText'),
          if (suffix != null) TextSpan(text: suffix, style: suffixStyle)
        ]));
  }
}
